package com.sugar.real.estate.module.business.dal.mysql.info;

import cn.hutool.core.util.StrUtil;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.real.estate.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.real.estate.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.real.estate.module.business.controller.admin.info.vo.InfoPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.info.vo.InfoRespVO;
import com.sugar.real.estate.module.business.controller.app.info.vo.AppInfoPageReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.info.InfoDO;
import com.sugar.real.estate.module.business.dal.dataobject.infocls.InfoClsDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 资讯信息 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface InfoMapper extends BaseMapperX<InfoDO> {

    default PageResult<InfoRespVO> selectPage(InfoPageReqVO reqVO) {
        String title = reqVO.getTitleZhCn();
        String content = reqVO.getContentZhCn();
        return selectJoinPage(reqVO, InfoRespVO.class, new MPJLambdaWrapperX<InfoDO>()
                .selectAll(InfoDO.class)
                .selectAs(InfoClsDO::getTitleZhCn, InfoRespVO::getClsNameZhCn)
                .selectAs(InfoClsDO::getTitleEnUs, InfoRespVO::getClsNameEnUs)
                .leftJoin(InfoClsDO.class, InfoClsDO::getId, InfoDO::getClsId)
                .eqIfPresent(InfoDO::getClsId, reqVO.getClsId())
                .betweenIfPresent(InfoDO::getReleaseDate, reqVO.getReleaseDate())
                .betweenIfPresent(InfoDO::getCreateTime, reqVO.getCreateTime())
                .nested(StrUtil.isNotBlank(title),
                        o -> o.like(InfoDO::getTitleZhCn, title).or().like(InfoDO::getTitleEnUs, title))
                .nested(StrUtil.isNotBlank(content),
                        o -> o.like(InfoDO::getContentZhCn, content).or().like(InfoDO::getContentEnUs, content))
                .orderByDesc(InfoDO::getId));
    }

    default PageResult<InfoDO> appSelectInfos(AppInfoPageReqVO reqVO){
        String search = reqVO.getSearch();
        return selectPage(reqVO, new LambdaQueryWrapperX<InfoDO>()
                .eqIfPresent(InfoDO::getClsId, reqVO.getClsId())
                .nested(StrUtil.isNotBlank(search), o -> o
                        .like(InfoDO::getTitleZhCn, search).or()
                        .like(InfoDO::getTitleEnUs, search).or()
                        .like(InfoDO::getContentZhCn, search).or()
                        .like(InfoDO::getContentEnUs, search))
                .orderByDesc(InfoDO::getId));
    }
}